package com.wechart.service.report.impl;


import com.wechart.dao.domain.Packageinf;
import com.wechart.dao.domain.PackageinfCriteria;
import com.wechart.dao.mapper.PackageinfMapper;
import com.wechart.dao.mapper.ext.PackageinfExtMapper;
import com.wechart.model.ReportModel;
import com.wechart.service.report.PackageinfoService;
import com.wechart.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


/**
 * Created by silenceholic on 2017/5/2.
 */
@Service(value = "packageinfoService")
public class PackageinfoServiceImpl implements PackageinfoService {

    @Autowired
    private PackageinfMapper packageinfMapper;

    @Autowired
    private PackageinfExtMapper packageinfExtMapper;


    @Override
    public List<Packageinf> getAllPackageinfo(ReportModel reportModel){
        PackageinfCriteria packageinfCriteria=new PackageinfCriteria();
        PackageinfCriteria.Criteria criteria=packageinfCriteria.createCriteria();
        if(StringUtils.isNoNullString(reportModel.getBegin())){
            criteria.andWorkdateGreaterThan(StringUtils.cutDateStr(reportModel.getBegin()));
        }
        if(StringUtils.isNoNullString(reportModel.getEnd())){
            criteria.andWorkdateLessThan(StringUtils.cutDateStr(reportModel.getEnd()));
        }
        if(StringUtils.isNoNullString(reportModel.getRefid())){
            criteria.andRefidEqualTo(reportModel.getRefid());
        }
        if(StringUtils.isNoNullString(reportModel.getSender())){
            criteria.andOppositeEqualTo(reportModel.getSender());
        }
        packageinfCriteria.setOrderByClause("workdate desc");
        return packageinfMapper.selectByExample(packageinfCriteria);
    }

    /**
     * 基本的count
     * @param reportModel
     * @return
     */
    @Override
    public int countPackageinfo(ReportModel reportModel){
        PackageinfCriteria packageinfCriteria=new PackageinfCriteria();
        PackageinfCriteria.Criteria criteria=packageinfCriteria.createCriteria();
        if(StringUtils.isNoNullString(reportModel.getBegin())){
            criteria.andWorkdateGreaterThan(StringUtils.cutDateStr(reportModel.getBegin()));
        }
        if(StringUtils.isNoNullString(reportModel.getEnd())){
            criteria.andWorkdateLessThan(StringUtils.cutDateStr(reportModel.getEnd()));
        }
        if(StringUtils.isNoNullString(reportModel.getRefid())){
            criteria.andRefidEqualTo(reportModel.getRefid());
        }
        if(StringUtils.isNoNullString(reportModel.getOpposite())){
            criteria.andOppositeEqualTo(reportModel.getOpposite());
        }
        packageinfCriteria.setOrderByClause("workdate desc");
        return packageinfMapper.countByExample(packageinfCriteria);
    }

    /**
     * 批量业务物理分页
     * @param reportModel
     * @return
     */
    public List<Packageinf> selectPackageinfoByPage(ReportModel reportModel){
        String begin=StringUtils.cutDateStr(reportModel.getBegin());
        String end=StringUtils.cutDateStr(reportModel.getEnd());

        List<Packageinf> packageinfs = packageinfExtMapper.selectPackageinfo(begin,end,reportModel.getRefid(),reportModel.getOpposite(),
                reportModel.getStart(),reportModel.getLength());

        return packageinfs;
    }


}
